//
//  BTEHomeViewController.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTESettingViewController: BTEBaseViewController {
    
    var model: BTEMEModel?
    
    fileprivate let server = BTEMeToolNetServer()
    
    var v: BTESettingView {
        return view as! BTESettingView
    }
    
    override func loadView() {
        view = BTESettingView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        updateNavigationTitle(BTELanguageManager.shared.localizedString(forKey: "Settings"))
        
        v.i1.snp.remakeConstraints { make in
            make.top.equalTo(navigationBar.snp.bottom).offset(22)
            make.left.right.equalToSuperview().inset(16)
            let sc = UIScreen.main.bounds.width - 16 - 16 - 16
            make.height.equalTo(sc * v.i1.frame.height / v.i1.frame.width)
        }
        
        v.i2.isUserInteractionEnabled = true
        v.i3.isUserInteractionEnabled = true
        
        let a1 = UITapGestureRecognizer.init(target: self, action: #selector(out))
        let a2 = UITapGestureRecognizer.init(target: self, action: #selector(deletea))
        
        v.i2.addGestureRecognizer(a2)
        v.i3.addGestureRecognizer(a1)
    }
    
    
    @objc fileprivate func out(){
        let vc = BTESigonoutAlertViewController.init()
        vc.modalPresentationStyle = .overFullScreen
        present(vc, animated: false)
        vc.action = {[weak self] in
            BTEHUD.shared.showLoader(on: vc.view)
            self?.server.squushy {[weak self] result in
                switch result {
                case .success(let success):

                    vc.dismiss(animated: false)
                    
                    NotificationCenter.default.post(name: .logout, object: nil)
                    
                    BTEHUD.shared.showSuccess(success?.squushy ?? "", on: (self?.view)!)
                    UserDefaults.standard.set("", forKey: USER_TOKEN)
                    
                    self?.navigationController?.popToRootViewController(animated: false)
                    
                    if  let windowScene = UIApplication.shared.connectedScenes
                        .first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene,
                          let rootVC = windowScene.windows.first(where: { $0.isKeyWindow })?.rootViewController  as? UITabBarController {
                        rootVC.selectedIndex = 0
                        (rootVC as! BTEBaseTabBarController).basetabbar.la.image = UIImage.init(named: "gwerger432323232")
                        (rootVC as! BTEBaseTabBarController).basetabbar.ed.image = UIImage.init(named: "fwef32323232er23")
                    }
                    
                    break
                case .failure(let failure):
                    switch failure {
                    case .Error(c: let b, m: let bd):
                        BTEHUD.shared.showError(bd, on: vc.view)
                        break
                    default:
                        break
                    }
                    break
                }
            }
        }
 
    }
    
   @objc  fileprivate func deletea(){
       let ve = BTEDeleteViewController()
       ve.model = model
       navigationController?.pushViewController(ve, animated: true)
    }
    
}
